package org.apache.ibatis.common.type;

import java.io.Reader;
import java.sql.*;

/**
 * @Name ClobReaderTypeHandler
 * @Description: [SQL Clob -> JAVA Reader] 类型处理器
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-05-10 17:43:01
 **/
public class ClobReaderTypeHandler extends BaseTypeHandler<Reader> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Reader parameter, JdbcType jdbcType) throws SQLException {
        ps.setClob(i, parameter);
    }

    @Override
    public Reader getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return toReader(rs.getClob(columnName));
    }

    @Override
    public Reader getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return toReader(rs.getClob(columnIndex));
    }

    @Override
    public Reader getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return toReader(cs.getClob(columnIndex));
    }

    private Reader toReader(Clob clob) throws SQLException {
        if (clob == null) return null;
        return clob.getCharacterStream();
    }
}

